package com.example.hello.mapper;

import com.example.hello.entity.Dept;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 部门Mapper接口
 */
@Mapper
public interface DeptMapper {
    
    /**
     * 查询所有部门
     */
    @Select("SELECT * FROM dept ORDER BY update_time DESC")
    List<Dept> selectAll();
    
    /**
     * 根据ID查询部门
     */
    @Select("SELECT * FROM dept WHERE id = #{id}")
    Dept selectById(@Param("id") Integer id);
    
    /**
     * 分页查询部门列表
     */
    @Select("<script>" +
            "SELECT * FROM dept " +
            "<where>" +
            "<if test='name != null and name != \"\"'>" +
            "AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "</where>" +
            "ORDER BY update_time DESC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<Dept> selectByPage(@Param("name") String name, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);
    
    /**
     * 查询总记录数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM dept " +
            "<where>" +
            "<if test='name != null and name != \"\"'>" +
            "AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "</where>" +
            "</script>")
    Long selectTotal(@Param("name") String name);
    
    /**
     * 添加部门
     */
    @Insert("INSERT INTO dept (name, create_time, update_time) VALUES (#{name}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Dept dept);
    
    /**
     * 修改部门
     */
    @Update("UPDATE dept SET name = #{name}, update_time = #{updateTime} WHERE id = #{id}")
    int update(Dept dept);
    
    /**
     * 删除部门
     */
    @Delete("DELETE FROM dept WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);
}
